<?php

namespace App\Models\V1;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use App\Models\V1\ChapterModel;

class CloMemberInfo extends Model
{
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    protected $table = 'my_clo_member_user_info';
    protected $primaryKey = 'clo_id';
    protected $fillable = ['user_id', 'flower', 'introduce', 'title', 'user_img'];


    public function get_member_info(Request $request)
    {
        $user_info  = DB::table('my_clo_member_user_info as my')
            ->leftJoin('memberinfo as m', 'm.UserID', '=', 'my.user_id')
            ->leftJoin("region as ct",'ct.region_id',"=","m.city")
            ->leftJoin('region as pr','pr.region_id','=','ct.parent_id')
            ->select(
                'clo_id',
                'user_id',
                'flower',
                'introduce',
                'title',
                'user_img',
                'm.YourName as name',
                'flower_user',
                'ct.region_name as city',
                'pr.region_name as province')
            ->whereNull('my.deleted_at')
            ->get();

        $user_phone = $request->userPhone;
        $citys      = ['上海市','北京市','重庆市','天津市'];
        foreach ($user_info as $k => &$v) {

            $v->city     = $v->city ? $v->city : "";
            $v->province = $v->province ? $v->province : "中国";
            $v->province = in_array($v,$citys) ? "中国" : $v->province;
            $flower_user = $v->flower_user;
            $v->user_img = change_member_head_img($v->user_img);
            $data        = explode(',', $flower_user);
            if (in_array($user_phone, $data)) {
                $v->flower_status = 1;
                unset($v->flower_user);
            } else {
                unset($v->flower_user);
                $v->flower_status = 0;
            }
        }


        $chapterModel = new ChapterModel();
        foreach ($user_info as $key => $value) {
            $user_info_role = $chapterModel->get_user_role($value->user_id);
            if ($user_info_role->role == 1) {
                $value->role = '普通会员';
            } elseif ($user_info_role->role == 2) {
                $value->role = '学习官';
            } elseif ($user_info_role->role == 3) {
                $value->role = '副会长';
            } elseif ($user_info_role->role == 4) {
                $value->role = '会长';
            }
        }
        return $user_info;
    }

    public function get_drp_img($user_id)
    {
        $memberInfo = MemberModel::where('UserID', $user_id)->select('FilePath1')->first();
        $user_img   = $memberInfo ? change_member_head_img($memberInfo->FilePath1) : null;
        return $user_img;
    }

}
